using UnityEngine;
using System.Collections;

public class Monster : MonoBehaviour
{

    public State state;

    private int life;
    private bool    moveStatic;
    private Vector3 moveDirection;
    private Vector3 startScale;
    private Vector3 dangerScale;
    private Vector3 normalScale;
    private Vector3 startPosition;
    private Vector3 targetPosition;
    private float   scaleSpeed;
    private bool    isDanger;
    private int     a;

    public MonsterTarget monsterTarget;
    public int           speed = 2;
    public Transform     child;
    
    void Awake ()
    {
        state = State.normal;
        moveStatic = false;
        isDanger = false;
        
        moveDirection = Vector3.forward;
        startScale = new Vector3 (1, 1, 1);
        dangerScale = new Vector3 (0.4f, 1.6f, 0.4f);
        normalScale = new Vector3 (1.64f, 0.4f, 1.6f);

        scaleSpeed = 0;
        a = 5;

        life = 4;
        //StartCoroutine("ChangeMoveDirection");
    }

    void Start ()
    {
        speed = GlobalManager.level + 1;
        if (GlobalManager.level == 2)
            life = 2;
        if (GlobalManager.level == 2)
            life = 1;
    }

    void Update ()
    {
        //动画
        if (state != State.death)
        {
            if (state == State.normal)
            {
                child.transform.localScale = Vector3.Lerp (startScale, normalScale, scaleSpeed);
            }
            if (state == State.danger)
            {
                child.transform.localScale = Vector3.Lerp (startScale, dangerScale, scaleSpeed);
            }

            scaleSpeed += Time.deltaTime * a;

            if (scaleSpeed > 1)
            {
                scaleSpeed = 1;
                a *= -1;
            }
            if (scaleSpeed < 0)
            {
                scaleSpeed = 0;
                a *= -1;
            }
        }

        //通过target移动
        if (state != State.death)
        {
            if (!moveStatic)
            {
                moveStatic = true;
                monsterTarget.transform.position = this.transform.position + moveDirection;
                monsterTarget.transform.position = new Vector3 (Mathf.RoundToInt (monsterTarget.transform.position.x),
                                                                Mathf.RoundToInt (monsterTarget.transform.position.y),
                                                                Mathf.RoundToInt (monsterTarget.transform.position.z));
                monsterTarget.StartMove ();
                monsterTarget.gameObject.GetComponent<Rigidbody> ().detectCollisions = true;
                if (moveDirection == Vector3.left)
                {
                    this.transform.rotation = GameController.gameController.left;
                }
                if (moveDirection == Vector3.right)
                {
                    this.transform.rotation = GameController.gameController.right;
                }
                if (moveDirection == Vector3.back)
                {
                    this.transform.rotation = GameController.gameController.back;
                }
                if (moveDirection == Vector3.forward)
                {
                    this.transform.rotation = GameController.gameController.forward;
                }
            }
        }
        
    }

    //随机改变方向
    public void ChangeDirection ()
    {
        int i; 
        i = Random.Range (0, 4);
        switch (i)
        {
        case 0:
            moveDirection = Vector3.left;
            break;
        case 1:
            moveDirection = Vector3.right;
            break;
        case 2:
            moveDirection = Vector3.forward;
            break;
        case 3:
            moveDirection = Vector3.back;
            break;
        }
    }

    //停止移动
    public void StopMove ()
    {
        moveStatic = false;
    }

    //与玩家碰撞
    void OnTriggerEnter (Collider player)
    {
        if (player != null)
        {
            if (state == State.normal)
            {
                GameController.gameController.Lose ();
            }
            if (state == State.danger)
            {
                GameController.gameController.score += 100;
                StartCoroutine ("Death");
            }
        }

    }

    //进入危险状态（不是死亡状态，生命大于0）
    public void Danger ()
    {
        if (state != State.death)
        {
            if (life > 0)
            {
                if (isDanger)
                {
                    StopCoroutine ("DangerState");
                }
                StartCoroutine ("DangerState");
            }
        }
    }

    //危险携程
    IEnumerator DangerState ()
    {
        life--;
        isDanger = true;
        state = State.danger;

        //10秒后结束危险状态
        yield return new WaitForSeconds (10);
        if (state != State.death)
        {
            state = State.normal;
        }
        isDanger = false;
    }

    //死亡携程
    IEnumerator Death ()
    {
        state = State.death;
        child.gameObject.GetComponent<MeshRenderer> ().enabled = false;
        startScale = this.transform.position;
        scaleSpeed = 0;

        //飘回老巢
        while (scaleSpeed < 1)
        {
            this.transform.position = Vector3.Lerp (startScale, GameController.gameController.monsterController.position, scaleSpeed);
            yield return 0;
            scaleSpeed += Time.deltaTime * 0.1F * (Vector3.Distance(startScale,GameController.gameController.monsterController.position)+1);
        }
        
        child.gameObject.GetComponent<MeshRenderer> ().enabled = true;
        state = State.normal;
        startScale = Vector3.one;
    }
}

public enum State
{
    normal,
    danger,
    death
}